package com.wu.gumomallproduct.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wu.gumomallproduct.product.service.AttrAttrgroupRelationService;
import com.wu.gumomallproduct.product.service.AttrService;
import com.wu.common.vo.product.AttrGroupRelationVo;
import com.wu.common.vo.product.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wu.common.entity.product.AttrGroupEntity;
import com.wu.gumomallproduct.product.service.AttrGroupService;
import com.wu.common.utils.PageUtils;
import com.wu.common.utils.R;



/**
 * ???Է??
 *
 * @author wu
 * @email 2195361340@qq.com
 * @date 2022-12-12 17:36:43
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    @RequestMapping("/{catelogId}/withattr")
    public R addRelation(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> data= attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", data);
    }
    /**
     * 新增
     */
    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrAttrgroupRelationService.addRelation(vos);
        return R.ok();
    }
    /**
     * 移除
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrAttrgroupRelationService.deleteRelation(vos);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R attrNoList(@PathVariable("attrgroupId") Long attrgroupId,
                        @RequestParam Map<String, Object> param){
        return R.ok().put("page", attrService.getAttrNoList(attrgroupId, param));
    }
    /**
     * 列表
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrList(@PathVariable("attrgroupId") Long attrgroupId){
        return R.ok().put("data", attrService.getAttrList(attrgroupId));
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getByAttrGroupId(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
